import { createRouter, createWebHistory } from 'vue-router'
import NotAuth from '@/views/not-auth.vue'
import Home from '@/views/home.vue'
import MainLayout from '@/layout/MainLayout/MainLayout.vue'
import AdminOrder from '@/views/admin/order.vue'
import Score from '@/views/score.vue'
// import PDefault from '@/layout/MainLayout/pDefault.vue'
// import PHome from '@/views/phone/home.vue'
// import PScore from '@/views/phone/score.vue'
import { isMobile } from '@/utils/common'
import Work from '@/views/work.vue'
import WorkMessage from '@/views/workMessage.vue'
import AdminWork from '@/views/admin/work.vue'

export const baseRoutes = [
  {
    path: '/not-auth',
    name: 'not-auth',
    component: NotAuth,
    meta: {
      title: '没有权限',
      icon: 'UserFilled',
      type: '1',
    },
    children: [],
  },
  {
    path: '/',
    name: 'root',
    component: MainLayout,
    meta: {},
    children: [
      {
        path: 'home',
        name: 'home',
        component: Home,
        meta: {
          title: '首页',
          icon: 'UserFilled',
          type: '1',
        },
        children: [],
      },
      {
        path: 'adminOrder',
        name: 'adminOrder',
        component: AdminOrder,
        meta: {
          title: '管理员订单',
          icon: 'UserFilled',
          type: '1',
        },
        children: [],
      },
      {
        path: 'score',
        name: 'score',
        component: Score,
        meta: {
          title: '购买会员',
          icon: 'UserFilled',
          type: '1',
        },
        children: [],
      },
      {
        path: 'work',
        name: 'work',
        component: Work,
        meta: {
          title: '工单',
          icon: 'UserFilled',
          type: '1',
        },
        children: [],
      },
      {
        path: 'workMessage',
        name: 'workMessage',
        component: WorkMessage,
        meta: {
          title: '工单内容',
          icon: 'UserFilled',
          type: '1',
        },
        children: [],
      },
      {
        path: 'adminWork',
        name: 'adminWork',
        component: AdminWork,
        meta: {
          title: '管理员工单',
          icon: 'UserFilled',
          type: '1',
        },
        children: [],
      },
    ],
  },
  {
    path: '/pHome',
    meta: {
      title: '首页',
    },
    component: () => import('@/layout/MainLayout/pDefault.vue'),
    children: [
      {
        path: 'index',
        component: () => import('@/views/phone/home.vue'),
        meta: {
          title: '首页',
        },
      },
      {
        path: 'score',
        component: () => import('@/views/phone/score.vue'),
        meta: {
          title: '充值会员',
        },
        children: [],
      },
    ],
  },
]
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [...baseRoutes],
})

export const indexRoute = '/home'
export const indexPhoneRoute = '/pHome/index'

const witeRoutes: any = ['/login', '/logout']
const toIndex = isMobile() ? indexPhoneRoute : indexRoute

// 全局路由守卫
router.beforeEach((to, from, next) => {
  if (witeRoutes.indexOf(to.path) >= 0) {
    next()
  } else {
    if (to.path == '/') {
      next(toIndex)
    } else {
      if (to.path.indexOf('pHome') >= 0 && !isMobile()) {
        next(indexRoute)
      } else if (to.path.indexOf('pHome') < 0 && isMobile()) {
        next(indexPhoneRoute)
      } else {
        next()
      }
    }
  }
})

// const hasRouteFun = (path: any) => {
//   const routes = router.getRoutes()
//   let hasRoute = false
//   routes.forEach((route: any) => {
//     if (route.path == path) {
//       hasRoute = true
//     }
//   })
//   return hasRoute
// }

export default router
